﻿using System;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace KingdeeWebServiceProject
{
    public class ESBSmsService
    {
        private string strUrl = "http://10.0.39.60:80/ESB_HTTP/InputProxy/LPS";        //正式
        //private string strUrl = "http://10.0.38.180:28005/ESB_HTTP/InputProxy/LPS";    //测试

        public string SendSmsService(string MobileNo, string SmsContent)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
            request.Method = "POST";
            request.Timeout = 60000;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.ContentType = "application/json";

            JObject Params = new JObject();
            Params.Add("ServiceCode", "CGIS000010");
            Params.Add("ChannelId", "XY00001");
            Params.Add("ExternalReference", "XY0000120120409123456789");
            Params.Add("OriginalChannelId", null);
            Params.Add("OriginalReference", null);
            Params.Add("RequestTime", DateTime.Now.AddHours(1).ToString("yyyyMMddHHmmss"));
            Params.Add("RequestBranchCode", "String");
            Params.Add("TermType", "String");
            Params.Add("TermNo", "String");

            JObject Data = new JObject();
            Data.Add("OperID", "iport24");
            Data.Add("OperPass", "iportbxhyt251");
            Data.Add("SendTime", null);
            Data.Add("ValidTime", null);
            Data.Add("AppendID", "");
            Data.Add("DesMobile", MobileNo);//"18681558329");
            Data.Add("Content", SmsContent);

            Params.Add("Data", Data);

            string strMsg = string.Format("短信地址：{0}。  ", strUrl);
            strMsg = string.Format("{0}  发送内容：{1}。  ", strMsg, Params.ToString());

            //try
            //{
                byte[] byteParas = Encoding.UTF8.GetBytes(Params.ToString());
                request.ContentLength = byteParas.Length;
                request.GetRequestStream().Write(byteParas, 0, byteParas.Length);
                request.GetRequestStream().Close();

                HttpWebResponse myResponseStream = (HttpWebResponse)request.GetResponse();
                StreamReader myStreamReader = new StreamReader(myResponseStream.GetResponseStream(), Encoding.UTF8);
                string strReturn = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                strMsg = string.Format("{0}  返回结果：{1}。  ", strMsg, strReturn);

                return strMsg;

                // return strReturn;
            //}
            //catch (Exception ex)
            //{
            //    strMsg = string.Format("{0}  返回结果：{1}。  ", strMsg, ex.Message);

            //    return strMsg;
            //    //return ex.Message;
            //}
        }
    }
}
